package com.yqg.manage.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yqg.manage.common.Result;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.controller.dto.PlaceDTO;
import com.yqg.manage.entity.Place;
import com.yqg.manage.entity.PlaceType;
import com.yqg.manage.mapper.PlaceMapper;
import com.yqg.manage.mapper.PlaceTypeMapper;
import com.yqg.manage.service.IPlaceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * PlaceController 前端控制器
 *
 * @author suzhuang
 * @since 2023-07-21
 */
@RestController
@RequestMapping("/place")
public class PlaceController {

    @Resource
    private IPlaceService placeService;

    @Resource
    private PlaceMapper placeMapper; // 注入PlaceMapper

    @Resource
    private PlaceTypeMapper placeTypeMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新企事业单位")
    public Result save(@RequestBody Place place) {
        if (place.getTypeName() != null && !place.getTypeName().equals("")) {
            PlaceType placeType = new PlaceType();
            placeType.setName(place.getTypeName());
            placeType.setPlaceDesc(place.getTypeName());
            placeTypeMapper.insert(placeType);
            place.setPlaceType(placeType.getId());
        }
        //判断信用代码的唯一性
        LambdaQueryWrapper<Place> placeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        placeLambdaQueryWrapper.eq(Place::getCompanyLicense,place.getCompanyLicense())
                .eq(Place::getIsDelete,"0");
        Place one = placeService.getOne(placeLambdaQueryWrapper);
        if(one != null && !one.getId().equals(place.getId())){
            return Result.error("信用代码或营业执照号已被其他企事业单位使用");
        }

        return Result.success(placeService.saveOrUpdate(place));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除企事业单位")
    public Result delete(@PathVariable Integer id) {
        Place place = placeService.getById(id);
        place.setIsDelete("1");
        return Result.success(placeService.saveOrUpdate(place));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Place> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Place> list = placeService.list();
        return Result.success(list);
    }

    /**
     * 查询一级大类
     */
    @GetMapping("/oneType")
    public Result findAllOneType() {
        QueryWrapper<Place> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0);
        queryWrapper.eq("is_delete", "0");
        List<Place> list = placeService.list(queryWrapper);
        return Result.success(list);
    }


    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(placeService.getById(id));
    }

    /**
     * 分页查询
     */
//    @GetMapping("/page")
//    public Result findPage(@RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize) {
//        QueryWrapper<Place> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        return Result.success(placeService.page(new Page<>(pageNum, pageSize), queryWrapper));
//    }
    @GetMapping("/findPage")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String param,
                           @RequestParam(required = false) Long typeId) {
        MPJLambdaWrapper<Place> placeMPJLambdaWrapper = new MPJLambdaWrapper<Place>()
                .selectAll(Place.class)
                .selectAs(PlaceType::getName, "typeName")
                .eq(Place::getIsDelete, "0")
                .and(StringUtils.isNotBlank(param), wq -> {
                    wq.like(Place::getCompanyName, param)
                            .or().like(Place::getCompanyAddress, param)
                            .or().like(Place::getCompanyLicense, param);
                })
                .leftJoin(PlaceType.class, PlaceType::getId, Place::getPlaceType);
        if (typeId != null) {
            placeMPJLambdaWrapper.eq(Place::getPlaceType, typeId);
        }
        return Result.success(placeService.page(new Page<>(pageNum, pageSize), placeMPJLambdaWrapper));
    }

    /**
     * 通过商户类型ID查询是否存在商户
     */
    @GetMapping("/findPlaceByType/{typeId}")
    public Result findPlaceByType(@PathVariable Long typeId) {
        QueryWrapper<Place> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("place_type", typeId);
        queryWrapper.eq("is_delete", "0");
        List<Place> list = placeService.list(queryWrapper);
        if (list != null && list.size() > 0) {
            return Result.success(true);
        } else {
            return Result.success(false);
        }
    }
}

